package com.bys.admin.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bys.response.R;
import com.bys.admin.system.controller.vo.SaveRoleMenuVo;
import com.bys.admin.system.entity.SysRoleMenu;
import com.bys.admin.system.service.ISysRoleMenuService;


import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色菜单表 前端控制器
 * </p>
 *
 * @author bys
 * @since 2025-02-13
 */
@Tag(name = "角色菜单接口")
@Validated
@RestController
@RequestMapping("/sys-role-menu")
public class SysRoleMenuController {

    @Resource
    private ISysRoleMenuService sysRoleMenuService;
    @Transactional(rollbackFor = Exception.class)
    @Operation(summary="保存角色权限")
    @PostMapping("/save/rights")
    public R<Boolean> saveRoleRights(@RequestBody @Valid SaveRoleMenuVo saveRoleMenuVo){
        sysRoleMenuService.remove(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId,saveRoleMenuVo.getRoleId()));
        List<SysRoleMenu> sysRoleMenuList = saveRoleMenuVo.getMenuIds().stream().map(menuId -> SysRoleMenu.builder().menuId(menuId).roleId(saveRoleMenuVo.getRoleId()).build()).collect(Collectors.toList());
        sysRoleMenuService.saveBatch(sysRoleMenuList);
        return R.success(Boolean.TRUE);
    }

    @Operation(summary="查询角色菜单")
    @GetMapping("/role/menu/{roleId}")
    public R<List<SysRoleMenu>> getRoleMenus(@PathVariable Long roleId){
        List<SysRoleMenu> sysRoleMenus = sysRoleMenuService.list(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, roleId));
        return R.success(sysRoleMenus);
    }


}
